package rise.ui;

import rise.comunity.Status;
import rise.comunity.UserData;
import rise.message.Logout;
import rise.net.LogoutTask;
import rise.net.OlivawPortsClient;
import rise.net.ReceiverService;
import rise.net.StatusNotificationTask;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

public abstract class RiseActivity extends Activity {

	protected Menu menu;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		this.menu = menu;

		getMenuInflater().inflate(R.menu.activity_rise, menu);

		UserData userData = UserData.obtainUserData();
		if (userData == null){
			menu.findItem(R.id.menu_busy).setVisible(false);
			menu.findItem(R.id.menu_online).setVisible(false);
			menu.findItem(R.id.menu_map).setVisible(false);
			menu.findItem(R.id.menu_logout).setVisible(false);
			return true;
		}

		Status status = userData.getStatus();
		if (status == Status.BUSY) {
			menu.findItem(R.id.menu_online).setChecked(false);
			menu.findItem(R.id.menu_busy).setChecked(true);
		} else {
			menu.findItem(R.id.menu_online).setChecked(true);
			menu.findItem(R.id.menu_busy).setChecked(false);
		}
		return true;
	}

	@Override
	protected void onRestart() {
		super.onResume();
		UserData userData = UserData.obtainUserData();
		if (userData == null)
			return;
		
		ReceiverService receiverService = ReceiverService
				.obtainReceiverService();
		if (!receiverService.isRunning())
			receiverService.startService();

		new StatusNotificationTask(this).execute(new Void[0]);
	}

	void superDotOnResume() {
		super.onResume();
	}
	
	void superDotOnRestart() {
		super.onRestart();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings:
			this.switchToSettings();
			break;
		case R.id.menu_online:
			this.changeStatusToOnline();
			break;
		case R.id.menu_busy:
			this.changeStatusToBusy();
			break;
		case R.id.menu_map:
			this.switchToMap();
			break;
		case R.id.menu_logout:
			this.logout();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void switchToSettings() {
		Intent intent = new Intent(this, Settings.class);
		this.startActivity(intent);
	}

	void changeStatusToOnline() {
		UserData userData = UserData.obtainUserData();
		Status originalStatus = userData.getStatus();
		if (originalStatus == Status.ONLINE)
			return;

		UserData.updateStatus(Status.ONLINE);

		new StatusNotificationTask(this).execute(new Void[0]);
		menu.findItem(R.id.menu_online).setChecked(true);
		menu.findItem(R.id.menu_busy).setChecked(false);
	}

	void changeStatusToBusy() {
		UserData userData = UserData.obtainUserData();
		Status originalStatus = userData.getStatus();
		if (originalStatus == Status.BUSY)
			return;

		UserData.updateStatus(Status.BUSY);
		//Log.e("-","OnClick Busy");

		new StatusNotificationTask(this).execute(new Void[0]);
		menu.findItem(R.id.menu_online).setChecked(false);
		menu.findItem(R.id.menu_busy).setChecked(true);
	}

	private void switchToMap() {
		Intent intent = new Intent(this, MapActivity.class);
		this.startActivity(intent);
	}

	void logout() {


		UserData userData = UserData.obtainUserData();
		Logout logout = new Logout(userData.getUsername(),
				OlivawPortsClient.getInputPort(), 0, 0);
		new LogoutTask(this, logout, this).execute(new Void[0]);

		//exitFromRise();
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationManager.cancelAll();
	}

	void exitFromRise() {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}

	void exit() {
		/*
		ReceiverService receiverService = ReceiverService
				.obtainReceiverService();
		receiverService.stopService();

		/*
		 * UserData userData = UserData.obtainUserData();
		 * userData.setStatus(Status.OFFLINE);
		 */

		/*new StatusNotificationTask(this, Status.OFFLINE).execute(new Void[0]);*/
		exitFromRise();
	}
}
